package com.watson.websocket.service1.impl;


import com.watson.websocket.config1.WebSocketConstant;
import com.watson.websocket.service1.WsSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

@Service
public class WsSendServiceImpl implements WsSendService {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @Override
    public void broadcastMsg(Object msg) {

        /**
         * 广播使用convertAndSend方法，第一个参数为目的地，和js中订阅的目的地要一致
         */
        simpMessagingTemplate.convertAndSend(WebSocketConstant.TOPIC_BROADCAST_MESSAGE, msg);

        /**
         *       点对点使用convertAndSendToUser方法，第一个参数为用户id，此时js中的订阅地址为
         *             "/user/" + 用户Id + "/message",其中"/user"是固定的
         */
        simpMessagingTemplate.convertAndSendToUser("zhangsan","/message",msg);
    }
}
